import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')

// 注册路由标签（占位符）
Vue.use(VueRouter)

const routes = [
  // 重定向到login页面
  {
    path: '/',
    redirect: '/login'
  },
  // login页面路由
  {
    path: '/login',
    // component: Login
    // 使用函数方法定义conponent路径
    component: () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Login.vue')
  },
  {
    path: '/home',
    component: Home,
    // 在home中重定向到welcome上
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Welcome.vue')
      },
      {
        path: '/users',
        component: () => import(/* webpackChunkName:"a" */ '@/components/user/Users.vue')
      },
      {
        path: '/rights',
        component: () => import(/* webpackChunkName:"a" */ '@/components/power/Rights.vue')
      },
      {
        path: '/roles',
        component: () => import(/* webpackChunkName:"a" */ '@/components/power/Roles.vue')
      },
      {
        path: '/categories',
        component: () => import(/* webpackChunkName:"b" */ '@/components/goods/Categories.vue')
      },
      {
        path: '/params',
        component: () => import(/* webpackChunkName:"b" */ '@/components/goods/Params.vue')
      },
      {
        path: '/goods',
        component: () => import(/* webpackChunkName:"c" */ '@/components/goods/Good_list.vue')
      },
      {
        path: '/goods/add',
        component: () => import(/* webpackChunkName:"c */ '@/components/goods/Addgoods.vue')
      },
      {
        path: '/orders',
        component: () => import(/* webpackChunkName:"d" */ '@/components/order/Orders.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫，防止输入地址强制跳转
router.beforeEach((to, from, next) => {
  // 如果要去的地址是login，可以直接跳转
  if (to.path === '/login') return next()
  // 如果不是login，就判断是否有token，没有token就跳转回login页面
  if (!sessionStorage.getItem('token')) return next('/login')
  // 如果不是login，就判断是否有token，有token就跳转到要去的页面
  next()
})

export default router
